import mysql, { PoolConnection } from "mysql2";

const pool = mysql.createPool({
  host: "59.110.162.232",
  user: "ball",
  password: "Dong10011q2w3e4r",
  database: "ball",
  waitForConnections: true,
  connectionLimit: 10,
  queueLimit: 0,
});

export const getConnection = (): Promise<PoolConnection> => {
  return new Promise((resolve, reject) => {
    pool.getConnection((err, connection) => {
      if (err) {
        reject(err);
      } else {
        resolve(connection);
      }
    });
  });
};

export const query = <T = Record<string, any>>(
  connection: PoolConnection,
  sql: string,
  values: any[] = []
): Promise<T[]> => {
  return new Promise((resolve, reject) => {
    connection.query(sql, values, (err, rows) => {
      if (err) {
        reject(err);
      } else {
        resolve(rows as T[]);
      }
    });
  });
};
